<template>
  <div class="execution" style="height:100%">
    <basic-container>
      <el-form :inline="true">
        <el-form-item label="日期">
          <el-date-picker
            v-model="searchForm.earningDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button type="primary" @click="handleExcelWrite">导 出 </el-button>
        </el-form-item>
      </el-form>
      <avue-crud
        ref="crud"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        :span-method="spanMethod"
      >
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { tableOption } from "@/const/crud/rca/swtvmcheckdetailreport";
import { handleExcelResponse } from "@/util/util";
import { dateFormat } from "@/util/date";
import request from "@/router/axios";

export default {
  name: "swtvmcheckdetailreport",
  data() {
    return {
      searchForm: {
        earningDate: dateFormat(new Date(), "yyyy-MM-dd")
      },
      tableData: [],
      tableLoading: false,
      tableOption: tableOption
    };
  },
  created() {
    const date = new Date();
    date.setTime(date.getTime() - 3600 * 1000 * 24);
    this.searchForm.earningDate = dateFormat(date, "yyyy-MM-dd");
    this.onSearch();
  },
  mounted() {
    this.tableOption.height = this.$el.offsetHeight - 160;
  },
  methods: {
    onSearch() {
      this.tableLoading = true;
      request({
        url:
          "/stm/swstreportquery/swStCheckDetails/" +
          this.searchForm.earningDate,
        method: "get"
      }).then(res => {
        if (res.data.data && res.data.data.length > 2) {
          this.tableData = res.data.data;
          this.tableLoading = false;
        }else{
          this.tableData = [];
          this.tableLoading = false;
        }
      });
    },
    // 导出
    async handleExcelWrite() {
      let res = await request({
        url:
          "/stm/swstreportquery/exportCheckDetails/" +
          this.searchForm.earningDate,
        method: "get",
        responseType: "blob",
        headers: {
          "Content-Type": "application/json;charset=UTF-8"
        }
      });
      handleExcelResponse(res, "车站TVM钱箱清点记录表.xlsx");
    },
    spanMethod({ row, column, rowIndex, columnIndex }) {
      if ((rowIndex + 1 === this.tableData.length && columnIndex === 2)||(rowIndex + 1 === this.tableData.length-1 && columnIndex === 2)) {
        return [1, 25];
      } else {
        return [1, 1];
      }
    }
  }
};
</script>
